namespace core {
    export class ResourceUtils extends BaseClass {
        private _configs: Array<any>;
        private _onConfigComplete: Function;
        private _onConfigCompleteTarget: any;

        private _groups: any;
        private _groupIndex: number = 0;

        public constructor() {
            super();

            this._configs = new Array<any>();
            this._groups = {};

            RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
            RES.addEventListener(RES.ResourceEvent.GROUP_PROGRESS, this.onResourceLoadProgress, this);
            RES.addEventListener(RES.ResourceEvent.GROUP_LOAD_ERROR, this.onResourceLoadError, this);
        }

        //加载配置文件
        public addConfig(jsonPath: string, filePath: string): void {
            this._configs.push([jsonPath, filePath]);
        }
        public loadConfig($onConfigComplete: Function, $onConfigCompleteTarget: any): void {
            this._onConfigComplete = $onConfigComplete;
            this._onConfigCompleteTarget = $onConfigCompleteTarget;
            this.loadNextConfig();
        }
        private loadNextConfig(): void {
            //加载完成
            if (this._configs.length == 0) {
                this._onConfigComplete.call(this._onConfigCompleteTarget);
                this._onConfigComplete = null;
                this._onConfigCompleteTarget = null;
                return;
            }

            var arr: any = this._configs.shift();
            RES.addEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.onConfigCompleteHandle, this);
            RES.loadConfig(arr[0], arr[1]);
        }
        private onConfigCompleteHandle(event: RES.ResourceEvent): void {
            RES.removeEventListener(RES.ResourceEvent.CONFIG_COMPLETE, this.onConfigCompleteHandle, this);
            this.loadNextConfig();
        }

        //加载资源
        public loadResource(resources = [], groups = [], onResourceLoadComplete: Function = null, onResourceLoadProgress: Function = null, onResourceLoadTarget: any = null): void {
            var needLoadArr = resources.concat(groups);
            var groupName = "loadGroup" + this._groupIndex++;
            this.loadGroups(groupName, needLoadArr, onResourceLoadComplete, onResourceLoadProgress, onResourceLoadTarget)
            //RES.createGroup(groupName, needLoadArr, true);
            //this._groups[groupName] = [$onResourceLoadComplete, $onResourceLoadProgress, $onResourceLoadTarget];
            //RES.loadGroup(groupName);
        }
        public loadGroup($groupName: string, $onResourceLoadComplete: Function, $onResourceLoadProgress: Function, $onResourceLoadTarget: any): void {
            this._groups[$groupName] = [$onResourceLoadComplete, $onResourceLoadProgress, $onResourceLoadTarget];
            RES.loadGroup($groupName);
        }
        public loadGroups($groupName: string, $subGroups: Array<any>, $onResourceLoadComplete: Function, $onResourceLoadProgress: Function, $onResourceLoadTarget: any): void {
            RES.createGroup($groupName, $subGroups, true);
            this.loadGroup($groupName, $onResourceLoadComplete, $onResourceLoadProgress, $onResourceLoadTarget)
        }
        private onResourceLoadComplete(event: RES.ResourceEvent): void {
            var groupName: string = event.groupName;
            if (this._groups[groupName]) {
                var loadComplete: Function = this._groups[groupName][0];
                var loadCompleteTarget: any = this._groups[groupName][2];
                if (loadComplete != null) {
                    loadComplete.apply(loadCompleteTarget, [groupName]);
                }

                this._groups[groupName] = null;
                delete this._groups[groupName];
            }
        }
        private onResourceLoadProgress(event: RES.ResourceEvent): void {
            var groupName: string = event.groupName;
            if (this._groups[groupName]) {
                var loadProgress: Function = this._groups[groupName][1];
                var loadProgressTarget: any = this._groups[groupName][2];
                if (loadProgress != null) {
                    loadProgress.call(loadProgressTarget, event.itemsLoaded, event.itemsTotal);
                }
            }
        }
        private onResourceLoadError(event: RES.ResourceEvent): void {
            console.debug(event.groupName + "资源组有资源加载失败");
            this.onResourceLoadComplete(event);
        }
    }
}